<?php

namespace app\service;

use app\dao\HonorDao;
use think\facade\Log;
use think\Exception;

class HonorService
{
    private $honorDao;

    public function __construct()
    {
        $this->honorDao = new HonorDao();
    }

    // 创建荣誉记录
    public function createHonor($userId, $type, $description, $filePath)
    {
        try {
            // 使用 HonorDao 创建荣誉记录
            $honor = $this->honorDao->create([
                'user_id' => $userId,
                'type' => $type,
                'description' => $description,
                'file_path' => $filePath
            ]);

            if (!$honor) {
                throw new Exception('创建荣誉记录失败');
            }

            return $honor;
        } catch (\Exception $e) {
            Log::error("创建荣誉记录失败: " . $e->getMessage());
            throw new Exception('系统异常，请稍后再试');
        }
    }

    // 获取成员的所有荣誉记录
    public function getHonorsByMemberId($memberId)
    {
        try {
            $honors = $this->honorDao->getAllHonorsByMemberId($memberId);

            if (!$honors) {
                throw new Exception('没有找到相关荣誉记录');
            }

            return $honors;
        } catch (\Exception $e) {
            Log::error("获取荣誉记录失败: " . $e->getMessage());
            throw new Exception('系统异常，请稍后再试');
        }
    }

    // 审核荣誉记录
    public function verifyHonor($id, $status, $userId)
    {
        try {
            $result = $this->honorDao->verifyHonor($id, $status, $userId);

            if (!$result) {
                throw new Exception('荣誉审核失败');
            }

            return ['msg' => '荣誉审核成功'];
        } catch (\Exception $e) {
            Log::error("荣誉审核失败: " . $e->getMessage());
            throw new Exception('系统异常，请稍后再试');
        }
    }
}
